#include "Skill.h"
#include "GameTest.h"
#include "GameMath.h"
#include "../../manager/PlayerManager.h"

Skill::Skill()
{
    timer.waitTime = 5;
    timer.onTimeout = [this] { isCooling = false; };
}

void Skill::OnUpdate(double delta)
{
    GameVector velocity = direction.Normalize() * speed * SIZE_TILE;
    position += velocity * delta;
    GameShape tempShape = shape;
    if (GameMath::Sign(angle, 90) == 0 || GameMath::Sign(angle, 270) == 0) {
        tempShape.w = shape.h;
        tempShape.h = shape.w;
    }
    for (const auto& enemy: GameInfo::Instance().enemies) {
        if (id == enemy->id) break;
        if (GameMath::CollisionDetection(position, tempShape, enemy->position, enemy->GetShape())) {
            enemy->DecreaseHp(damage);
            enemy->Rigidity();
            if (isOnce) {
                isValid = false;
            }
        }
    }
    auto player = PlayerManager::Instance().GetPlayer();
    if (id != player->id) {
        if (GameMath::CollisionDetection(position, tempShape, player->position, player->GetShape())) {
            player->DecreaseHp(damage);
            player->Rigidity();
            if (isOnce) {
                isValid = false;
            }
        }
    }
    animation.OnUpdate(delta);
}

void Skill::OnRender()
{
    if (needRotate) {
        animation.OnRender(GAME_RENDERER, position, angle);
    } else {
        animation.OnRender(GAME_RENDERER, position);
    }

    if (test_mode_flag) {
        GameShape tempShape = shape;
        if (GameMath::Sign(angle, 90) == 0 || GameMath::Sign(angle, 270) == 0) {
            tempShape.w = shape.h;
            tempShape.h = shape.w;
        }
        RendererRect(position, tempShape, {0, 0, 0, 255});
    }
}

bool Skill::AiCanRelease(const GameVector& targetPos)
{
    return false;
}